<?php
/**
 * (exporter)
 */
namespace App\Exporter\Products;

use App\Models\Product\ProductMixSku;
use App\Models\Product\ProductSku;
use App\Util\User;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithMapping;
class SkuPackingExporter extends ExcelExporter implements WithMapping
{
    protected $fileName = '产品包装尺寸.xlsx';

    protected $headings = ['SKU','组合','组合规则','是否在售','名称','品类','采购单价','不含税单价','采购周期','产品状态','负责人','SKU启用状态','产品长度(CM)','产品宽度(CM)','产品高度(CM)','产品重量(KG)',
        '包装长度(CM)','包装宽度(CM)','包装高度(CM)','发货重量(KG)','每PCS国内运费(元)'];

    public function map($model) : array
    {
        $mix = $model->sku->is_mix ?? 0;
        if($mix == 1){
            $data = ProductMixSku::getMixSkuPuCycleAndPuPriceAndNoTax($model->sku->mix_rule);
            $unitPrice = numberFormat($data['unit_price']);
            $unitPriceNoTax = numberFormat($data['unit_price_no_tax']);
            $purchaseCycle = numberFormat($data['purchase_cycle']);
        }else{
            $unitPrice = numberFormat($model->sku->product_supplier->unit_price ?? 0);
            $unitPriceNoTax = numberFormat(ProductSku::tax($model->sku->product_supplier->unit_price ?? 0,$model->sku->product_supplier->supplier->tax ?? 0));
            $purchaseCycle = numberFormat($model->sku->product_supplier->purchase_cycle ?? 0);
        }
        return [
            $model->sku->psku_code ?? '',
            ProductSku::$_isMix[$mix],
            $model->sku->mix_rule ?? '',
            ProductSku::$_isSales[$model->sku->is_sales ?? 0],
            $model->sku->psku_cn ?? '',
            $model->sku->classify->title ?? '',
            $unitPrice,
            $unitPriceNoTax,
            $purchaseCycle,
            isset($model->sku->psku_status)&&$model->sku->psku_status ? ProductSku::$_pSkuStatus[$model->sku->psku_status] : '',
            User::getUsernameById($model->sku->muser_id ?? 0),
            isset($model->sku->status)&&$model->sku->status ? ProductSku::$_status[$model->sku->status] : '',
            $model->sku_long,
            $model->sku_wide,
            $model->sku_high,
            $model->sku_weight,
            $model->pack_long,
            $model->pack_wide,
            $model->pack_high,
            $model->ship_weight,
            $model->fee_per_pcs,
        ];
    }

}
